package vn.edu.tdt.it.entity;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import vn.edu.tdt.it.common.VietnameseProcessing;
import vn.edu.tdt.it.domain.BuildOntologyFinal;
import vn.edu.tdt.it.domain.StaticConnection;

public class Page {
	int id;
	String name;
	public String nonNomalized;
	List<Page> equivalent = null;
	List<MyProperty> properties = null;

	public Page(int id, String name) {
		this.id = id;
		nonNomalized = name.replaceAll("_", " ");
		setName(name);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = VietnameseProcessing.removeSpecialChar(name);
	}

	public List<Page> getEquivalent() throws Exception {
		if (equivalent == null) {
			equivalent = new ArrayList<Page>();
			String sql = "SELECT id, name FROM pagemapline WHERE pageId = ?";
			PreparedStatement ps = StaticConnection.getPreparedStatement(sql);
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Page p = new Page(rs.getInt(1), rs.getString(2));
				equivalent.add(p);
			}
		}
		return equivalent;
	}

	public List<MyProperty> getProperties() throws Exception {
		if (properties == null) {
			properties = new ArrayList<MyProperty>();
			String sql = "SELECT id FROM entity WHERE title = ?";
			PreparedStatement ps = StaticConnection.getPreparedStatement(sql);
			ps.setString(1, nonNomalized);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				sql = "SELECT pid, txt FROM property WHERE id = ?";
				ps = StaticConnection.getPreparedStatement(sql);
				ps.setString(1, rs.getString(1));
				ResultSet rs2 = ps.executeQuery();
				while (rs2.next()) {
					sql = "SELECT title FROM entity WHERE id = ?";
					ps = StaticConnection.getPreparedStatement(sql);
					ps.setString(1, rs2.getString(2));
					ResultSet rs3 = ps.executeQuery();
					while (rs3.next()) {
						MyProperty p = new MyProperty(rs2.getString(1),
								BuildOntologyFinal.URI_OWL
										+ BuildOntologyFinal.SOURCE
										+ VietnameseProcessing
												.removeSpecialChar(rs3
														.getString(1)),
								rs3.getString(1));
						properties.add(p);
					}
				}
			}
			/*
			 * String sql =
			 * "SELECT p.pid, e.title FROM entity e INNER JOIN property p ON e.id = p.txt INNER JOIN ( Select id FROM entity WHERE title = ? ) j ON j.id = p.id"
			 * ; PreparedStatement ps =
			 * StaticConnection.getPreparedStatement(sql); ps.setString(1,
			 * nonNomalized); ResultSet rs = ps.executeQuery(); while
			 * (rs.next()) { MyProperty p = new MyProperty(rs.getString(1),
			 * BuildOntologyFinal.URI_OWL + BuildOntologyFinal.SOURCE +
			 * VietnameseProcessing .removeSpecialChar(rs .getString(2)));
			 * properties.add(p); }
			 */
		}

		return properties;
	}
}
